<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/inc/taglib.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>资源信息列表</title>
<%@ include file="/inc/head.jsp"%>
<script type="text/javascript">
	var grid;
	$(function() {
		$("#addDialog").dialog("close");
		$("#updateDialog").dialog("close");
		grid = $('#grid').treegrid(
				{
					title : '',
					method : "get",
					idField : 'id',
					treeField : 'name',
					url : '${ctx}/sys/organization/organization/listgrid?v='
							+ new Date(),
					rownumbers : true,
					pagination : false,
					columns : [ [ {
						width : '80',
						title : '选择',
						field : 'id',
						checkbox : true
					}, {
						width : '200',
						title : '组织名称',
						field : 'name'
					}, {
						width : '200',
						title : '图标名称',
						field : 'icon'
					}, {
						width : '200',
						title : '组织类型',
						field : 'type'
					}, {
						width : '200',
						title : 'pid',
						field : 'parentId',
						hidden : true
					} ] ],
					toolbar : '#toolbar',
					onBeforeLoad : function(row, param) {
						parent.$.messager.progress({
							text : '数据加载中....'
						});
					},
					onLoadSuccess : function(row, data) {
						parent.$.messager.progress('close');
					}
				});
	});

	function add() {
		$("#addDialog").dialog('open');
	}

	function redoFun() {
		var row = $("#grid").datagrid('getSelections');
		if (row.length > 0) {
			$.messager
					.confirm(
							"提示",
							"你确定要删除吗?",
							function(r) {
								if (r) {
									window.location.href = "${ctx}/sys/organization/organization/deleteMessage/"
											+ row[0].id;
								}
							});
		}
	}

	function update() {
		var row = $("#grid").datagrid('getSelections');
		console.info(row[0].id);
		$("#name").val(row[0].name);
		$("#icon").val(row[0].icon);
		$("#url").val(row[0].url);
		$("#identity").val(row[0].identity);
		$("#ids").val(row[0].id);
		var t1 = document.getElementById("parentid");
		for (i = 0; i < t1.length; i++) {//给select赋值 
			if (row[0].parentid == t1.options[i].value) {
				t1.options[i].selected = true;
			}
		}
		$("#updateDialog").dialog('open');
	}

	function ConvertToTreeGridJson(rows, idFieldName, pidFieldName, fileds) {
		function exists(rows, ParentId) {
			for ( var i = 0; i < rows.length; i++) {
				if (rows[i][idFieldName] == ParentId)
					return true;
			}
			return false;
		}
		var nodes = [];
		// get the top level nodes
		for ( var i = 0; i < rows.length; i++) {
			var row = rows[i];
			if (!exists(rows, row[pidFieldName])) {
				var data = {
					id : row[idFieldName]
				}
				var arrFiled = fileds.split(",");
				for ( var i = 0; i < arrFiled.length; i++) {
					if (arrFiled[i] != idFieldName)
						data[arrFiled[i]] = row[arrFiled[i]];
				}
				nodes.push(data);
			}
		}

		var toDo = [];
		for ( var i = 0; i < nodes.length; i++) {
			toDo.push(nodes[i]);
		}
		while (toDo.length) {
			var node = toDo.shift(); // the parent node
			// get the children nodes
			for ( var i = 0; i < rows.length; i++) {
				var row = rows[i];
				if (row[pidFieldName] == node.id) {
					var child = {
						id : row[idFieldName]
					};
					var arrFiled = fileds.split(",");
					for ( var j = 0; j < arrFiled.length; j++) {
						if (arrFiled[j] != idFieldName) {
							child[arrFiled[j]] = row[arrFiled[j]];
						}
					}
					if (node.children) {
						node.children.push(child);
					} else {
						node.children = [ child ];
					}
					toDo.push(child);
				}
			}
		}
		return nodes;
	};
</script>
</head>
<body class="easyui-layout" data-options="fit:true,border:false">
	<div id="toolbar" style="display: none;">
		<table>
			<tr>
				<td><a href="javascript:void(0);" class="easyui-linkbutton"
					data-options="iconCls:'icon-add',plain:true" onclick="add();">添加</a></td>
				<td><a href="javascript:void(0);" class="easyui-linkbutton"
					data-options="iconCls:'icon-edit',plain:true" onclick="update();">修改</a></td>
				<td><div class="datagrid-btn-separator"></div></td>
				<td><a onclick="redoFun();" href="javascript:void(0);"
					class="easyui-linkbutton"
					data-options="plain:true,iconCls:'icon-remove'">删除</a><a
					onclick="undoFun();" href="javascript:void(0);"
					class="easyui-linkbutton"
					data-options="plain:true,iconCls:'icon-redo'">折叠</a></td>
				<td><div class="datagrid-btn-separator"></div></td>
				<td><a onclick="grid.treegrid('reload');"
					href="javascript:void(0);" class="easyui-linkbutton"
					data-options="plain:true,iconCls:'icon-reload'">刷新</a></td>
			</tr>
		</table>
	</div>
	<div data-options="region:'center',fit:true,border:false">
		<table id="grid" data-options="fit:true,border:false"></table>
		<%--  <ul id="tt" class="easyui-tree"
                  url="${ctx}/sys/resource/info/queryAll">
             </ul> --%>
	</div>


	<div id="addDialog" class="easyui-dialog" title="添加用户信息"
		style="width: 350px; height: 300px; padding: 5px;"
		data-options="modal : true, topMost: false,buttons: [{
							text:'提交',
							iconCls:'icon-save',
							handler:function(){
							alert('21');
							$('#addForm').submit();
								}
							},{
							text:'取消',
							iconCls:'icon-cancel',
							handler:function(){
 							$('#addDialog').dialog('close');}}]">
		<form id="addForm" action="${ctx}/sys/organization/organization/add"
			method="post">
			<table>
				<tr>
					<td><label>组织名称</label></td>
					<td><input class="easyui-validatebox"
						data-options="required:true" type="text" name="name" /></td>
				</tr>
				<tr>
					<td><label>图标名称</label></td>
					<td><input class="easyui-validatebox"
						data-options="required:true" type="text" name="icon" /></td>
				</tr>
				<tr>
					<td><label>上级资源</label></td>
					<td><select name="parentId">
							<c:forEach var="n" items="${entity}">
								<option value="${n.id}">${n.name}</option>
							</c:forEach>
					</select></td>
				</tr>
			</table>
		</form>
	</div>

	<div id="updateDialog" class="easyui-dialog" title="修改用户信息"
		style="width: 350px; height: 300px; padding: 5px;"
		data-options="modal : true, topMost: false,buttons: [{
							text:'提交',
							iconCls:'icon-save',
							handler:function(){
							alert('21');
							$('#updateForm').submit();
								}
							},{
							text:'取消',
							iconCls:'icon-cancel',
							handler:function(){
 							$('#updateDialog').dialog('close');}}]">
		<form id="updateForm"
			action="${ctx}/sys/organization/organization/update" method="post">
			<table>
				<tr>
					<td><label>组织名称<input type="hidden" id="ids" name="id"></label></td>
					<td><input class="easyui-validatebox"
						data-options="required:true" type="text" name="name" id="name" /></td>
				</tr>
				<tr>
					<td><label>图标名称</label></td>
					<td><input class="easyui-validatebox"
						data-options="required:true" type="text" name="icon" id="icon" /></td>
				</tr>

				<tr>
					<td><label>上级资源</label></td>
					<td><select name="parentId" id="parentid">
							<c:forEach var="n" items="${entity}">
								<option value="${n.id}">${n.name}</option>
							</c:forEach>
					</select></td>
				</tr>
			</table>
		</form>
	</div>
</body>
</html>